##################################################################################################################################
#
# Copyright (C) 2008-2013 Mind System Connection. Lda. Part of Daidze Software, created by Siqsuruq. All other rights reserved.
#
# Mind System Connection Webpage: www.msysc.org
# Daidze Software Page: www.daidze.com www.daidze.org
# E-mail: admin@msysc.org, siqsuruq@gmail.com
#
##################################################################################################################################

proc add_stock_movement_type {} {
	set values [$::daidze_main get_values]
	
	if {[dict get $values name] eq "TRS"} {
		set msg_list [dict create name "Name is obligatory." description "Description is obligatory." init_warehouse "Initial warehouse is obligatory." final_warehouse "Final warehouse is obligatory."]
	} else {
		set msg_list [dict create name "Name is obligatory." description "Description is obligatory."]
	}
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		if {[dict get $values default_mov] eq "for_sale"} {
			if {[$::db select_all stock_movement_type * "sale_mov=\'1\'"] != ""} {
				set flag 1
			} else {
				set flag 0
				dict lappend values sale_mov 1
			}
		} elseif {[dict get $values default_mov] eq "for_buy"} {
			if {[$::db select_all stock_movement_type * "buy_mov=\'1\'"] != ""} {
				set flag 1
			} else {
				set flag 0
				dict lappend values buy_mov 1
			}
		} else {
			set flag 0
		}
		
		set values [dict remove $values default_mov]
		if {$flag != 0} {
			tk_messageBox -icon info -type ok -message "[::msgcat::mc "There is already default movement type. This one will be stored as generic type."]"
		}
		$::db transaction start
		if {[dict get $values name] eq "TRS"} {
			set init_warehouse [$::db select_id_by_name warehouse [dict get $values init_warehouse]]
			set final_warehouse [$::db select_id_by_name warehouse [dict get $values final_warehouse]]
			if {$init_warehouse ne $final_warehouse} {
				dict set values init_warehouse $init_warehouse
				dict set values final_warehouse $final_warehouse
				$::db insert_all stock_movement_type [dict create name "OUT" description "[dict get $values description]" transfer_mov 1]
				$::db insert_all stock_movement_type [dict create name "IN" description "[dict get $values description]" transfer_mov 1]
				dict lappend values transfer_mov 1
				$::db insert_all stock_movement_type $values
		
				$::db transaction stop
				$::daidze_main load_main stock stock_movement_type.xml $::main 1
			} else {
				tk_messageBox -icon info -type ok -message "[::msgcat::mc "Final warehouse cant be the same as initial."]"
				
			}
		} else {
			$::db insert_all stock_movement_type $values
			$::db transaction stop
			$::daidze_main load_main stock stock_movement_type.xml $::main 1
		}
	}
}

proc get_trs {} {
	set result [$::db select_all stock_movement_type {description} "name=\'TRS\'" list]
	return $result
}

proc get_mov {} {
	set result [$::db select_all stock_movement_type {description} "transfer_mov ISNULL AND sale_mov ISNULL" list]
	return $result
}

proc update_warehouses {w} {
	set desc [$w get]
	set warehouse_list [$::db select_all stock_movement_type {init_warehouse final_warehouse} "description=\'$desc\' AND init_warehouse NOTNULL AND final_warehouse NOTNULL" list]
	set init_warehouse [lindex [$::db select_name_by_id warehouse [lindex $warehouse_list 0]] 0]
	set final_warehouse [lindex [$::db select_name_by_id warehouse [lindex $warehouse_list 1]] 0]
	$::daidze_main update_form [dict create init_warehouse $init_warehouse final_warehouse $final_warehouse]
}

proc edit_stock_movement_type {table_var} {
	set sdata [dict create]
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]

	set vars {id name description}
	if {[$my_tablelist curselection] ne ""} {
		set row [$my_tablelist rowcget [$my_tablelist curselection] -text]
		foreach key $vars value $row {
			dict set sdata $key $value
		}
		
		set res [$::db select_all stock_movement_type * "description ='[dict get $sdata description]' and name='TRS'" list]
		if {$res ne ""} {
			dict set sdata name "TRS"
			dict set sdata init_warehouse [$::db select_name_by_id warehouse [lindex $res 6]]
			dict set sdata final_warehouse [$::db select_name_by_id warehouse [lindex $res 7]]
			dict set sdata default_mov general
			dict set sdata id [lindex $res 0] 
		}


		set def_type [$::db select_all stock_movement_type "sale_mov buy_mov" "description ='[dict get $sdata description]'" list]
		if {[lindex $def_type 0] ne ""} {
			dict set sdata default_mov for_sale
		} elseif {[lindex $def_type 1] ne ""} {
			dict set sdata default_mov for_buy
		} else {
			dict set sdata default_mov general
		}
		
		$::daidze_main load_main stock edit_stock_movement_type.xml $::main
		$::daidze_main update_form $sdata
		if {$res ne ""} {
			$::daidze_main state_widget {init_warehouse final_warehouse} normal 0
		}
	} else {
		tk_messageBox -icon info -type ok -message "[::msgcat::mc "Select movement type please."]"
	}
}

proc update_stock_movement_type {} {
	set values [$::daidze_main get_values]
	
	if {[dict get $values name] eq "TRS"} {
		set msg_list [dict create name "Name is obligatory." description "Description is obligatory." init_warehouse "Initial warehouse is obligatory." final_warehouse "Final warehouse is obligatory."]
	} else {
		set msg_list [dict create name "Name is obligatory." description "Description is obligatory."]
	}
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		if {[dict get $values default_mov] eq "for_sale"} {
			if {[$::db select_all stock_movement_type * "sale_mov=\'1\'"] != ""} {
				set flag 1
			} else {
				set flag 0
				dict lappend values sale_mov 1
			}
		} elseif {[dict get $values default_mov] eq "for_buy"} {
			if {[$::db select_all stock_movement_type * "buy_mov=\'1\'"] != ""} {
				set flag 1
			} else {
				set flag 0
				dict lappend values buy_mov 1
			}
		} else {
			set flag 0
		}
		
		set values [dict remove $values default_mov]	

		if {$flag != 0} {
			tk_messageBox -icon info -type ok -message "[::msgcat::mc "There is already default movement type. This one will be stored as generic type."]"
		}
		
		$::db transaction start
		if {[dict get $values name] eq "TRS"} {
			set init_warehouse [$::db select_id_by_name warehouse [dict get $values init_warehouse]]
			set final_warehouse [$::db select_id_by_name warehouse [dict get $values final_warehouse]]
			if {$init_warehouse ne $final_warehouse} {
				dict set values init_warehouse $init_warehouse
				dict set values final_warehouse $final_warehouse
				set old_desc [lindex [$::db select_all stock_movement_type description "id='[dict get $values id]'" list] 0]
				set in_id [$::db select_all stock_movement_type id "description ='$old_desc' AND transfer_mov ='1' AND name='IN'" list]
				set out_id [$::db select_all stock_movement_type id "description ='$old_desc' AND transfer_mov ='1' AND name='OUT'" list]

				dict lappend values transfer_mov 1
				$::db update_all stock_movement_type $values
				
				$::db update_all stock_movement_type [dict create id $out_id name "OUT" description "[dict get $values description]" transfer_mov 1]
				$::db update_all stock_movement_type [dict create id $in_id name "IN" description "[dict get $values description]" transfer_mov 1]

				$::db transaction stop
				$::daidze_main load_main stock stock_movement_type.xml $::main 1
			} else {
				tk_messageBox -icon info -type ok -message "[::msgcat::mc "Final warehouse cant be the same as initial."]"
				
			}
		} else {
			$::db update_all stock_movement_type $values
			$::db transaction stop
			$::daidze_main load_main stock stock_movement_type.xml $::main 1
		}
	}
}

proc add_new_stock_line {} {
	set new_stock_l [top_win "Add new line to stock"]
	set stock_line_frame [ttk::frame $new_stock_l.top]
	set ::daidze_nstline [xml_wrapper new]
	pack $stock_line_frame
	
	$::daidze_nstline load_main stock add_new_stock_line.xml $stock_line_frame
}

proc add_transfer_line_form {} {
	set new_stock_l [top_win "Add products"]
	set stock_line_frame [ttk::frame $new_stock_l.top]
	set ::daidze_nstline [xml_wrapper new]
	pack $stock_line_frame
	$::daidze_nstline load_main stock add_transfer_line.xml $stock_line_frame
}

proc add_transfer_line {} {
	set values [$::daidze_nstline get_values]

	set msg_list [dict create qty "Quantity is obligatory." fk_product "Product field is obligatory."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {

		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 
	
	if {[lsearch -not -exact $flags 1] == "-1"} {
		dict create stock_lines $values
		$::daidze_main update_form [dict create stock_lines [dict create 0 $values] table_clear 0]
		destroy .topwin
	}
}

proc add_stock_line {} {
	set values [$::daidze_nstline get_values]

	set msg_list [dict create qty "Quantity is obligatory." fk_product "Product field is obligatory." stock_movement_type "Movement type is obligatory."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {

		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 
	
	if {[lsearch -not -exact $flags 1] == "-1"} {
		dict create stock_lines $values
		$::daidze_main update_form [dict create stock_lines [dict create 0 $values] table_clear 0]
		destroy .topwin
	}
}

proc calculate_total_product_cost {w} {
	set values [$::daidze_nstline get_values list cost_product cost_tax cost_ship cost_other]
	set result 0
	foreach val $values {
		if {$val eq ""} {
			set result [expr $result + 0]
		} else {
			set result [expr $result + $val]
		}
	}
	set qty [lindex [$::daidze_nstline get_values list qty] 0]
	if {$qty ne ""} {
		set result [expr $qty * $result]
	} 
	$::daidze_nstline update_form [dict create cost_total $result]
}

proc check_type {w} {
	set stype [$w get]
	if {$stype eq "TRS"} {
		$::daidze_main state_widget {init_warehouse final_warehouse} readonly
	} else {
		$::daidze_main state_widget {init_warehouse final_warehouse} disabled 1
	}
}

proc add_transfer_movement {} {
	[dict get [$::daidze_main get_fdata] stock_lines] selection set 0 end
	set values [$::daidze_main get_values]

	set msg_list [dict create stock_movement_type "Transferring is obligatory." mov_date "Date is obligatory." stock_lines "Add at least one product to transfer."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		set stock_lines [dict get $values stock_lines]

		$::db transaction start
		set init_warehouse [dict get $values init_warehouse]
		set mov_type [lindex [$::db select_all stock_movement_type id "description=\'[dict get $values stock_movement_type]\' AND name=\'OUT\'"] 1]
		foreach line [dict values $stock_lines] {
			set tmp [dict create]
			dict append tmp fk_warehouse $init_warehouse
			dict append tmp mov_date [dict get $values mov_date]
			dict append tmp mov_time [dict get $values mov_time]
			dict append tmp fk_product [dict get $line fk_product]
			dict append tmp qty [dict get $line qty]
			dict append tmp stock_movement_type [lindex $mov_type 1]
			$::db insert_all stock_current $tmp
		}	
		
		
		set final_warehouse [dict get $values final_warehouse]
		set mov_type [lindex [$::db select_all stock_movement_type id "description=\'[dict get $values stock_movement_type]\' AND name=\'IN\'"] 1]
		foreach line [dict values $stock_lines] {
			set tmp [dict create]
			dict append tmp fk_warehouse $final_warehouse
			dict append tmp mov_date [dict get $values mov_date]
			dict append tmp mov_time [dict get $values mov_time]
			dict append tmp fk_product [dict get $line fk_product]
			dict append tmp qty [dict get $line qty]
			dict append tmp stock_movement_type [lindex $mov_type 1]
			$::db insert_all stock_current $tmp
		}
		$::db transaction stop
		$::daidze_main load_main stock stock.xml $::main 1
	}
}

proc check_product_on_stock {warehouse product} {
	set result [$::db select_all on_stock stock_now "warehouse_name=\'$warehouse\' AND product_name=\'$product\'" list]
	return $result
}

proc add_stock_movement {} {
	[dict get [$::daidze_main get_fdata] stock_lines] selection set 0 end
	set values [$::daidze_main get_values]

	set msg_list [dict create fk_warehouse "Warehouse name is obligatory." mov_date "Date is obligatory." stock_lines "Add at least one movement"]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		set stock_lines [dict get $values stock_lines]
		foreach line [dict values $stock_lines] {
			set tmp [dict create]
			set mov_type [lindex [$::db select_all stock_movement_type id "description=\'[dict get $line stock_movement_type]\'"] 1]
			dict append tmp fk_warehouse [dict get $values fk_warehouse]
			dict append tmp mov_date [dict get $values mov_date]
			dict append tmp mov_time [dict get $values mov_time]
			dict append tmp fk_product [dict get $line fk_product]
			dict append tmp qty [dict get $line qty]
			dict append tmp stock_movement_type [lindex $mov_type 1]
			dict append tmp cost_product [dict get $line cost_product]
			dict append tmp cost_tax [dict get $line cost_tax]
			dict append tmp cost_ship [dict get $line cost_ship]
			dict append tmp cost_other [dict get $line cost_other]
			dict append tmp cost_total [dict get $line cost_total]

			$::db insert_all stock_current $tmp
		}
		$::daidze_main load_main stock stock.xml $::main 1
	}
}

proc delete_movement {} {
	set values [lindex [$::daidze_main get_values list stock] 0]
	if {[set id_to_delete [lindex [lindex $values 0] 0]] ne ""} {
		$::db delete_row stock_current $id_to_delete
		$::daidze_main load_main stock stock.xml $::main 1
	}
}

proc edit_movement {table_var} {
	set sdata [dict create]
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]

	set vars {id fk_warehouse product_name qty mov_date mov_time description}
	if {[$my_tablelist curselection] ne ""} {
		set row [$my_tablelist rowcget [$my_tablelist curselection] -text]
		foreach key $vars value $row {
			dict set sdata $key $value
		}
	
		set mov_types [lindex [$::db select_all stock_movement_type "sale_mov" "description = \'[dict get $sdata description]\'" list] 0]
		if {$mov_types ne ""} {
			 tk_messageBox -icon info -type ok -message "[::msgcat::mc "You can not edit sale movement directly."]"
		} else {

			set tbl_line [dict create]

			dict set tbl_line qty [dict get $sdata qty]
			dict set tbl_line fk_product [dict get $sdata product_name]
			dict set tbl_line stock_movement_type [dict get $sdata description]

			set result [$::db select_all stock_current {cost_product cost_tax cost_ship cost_other cost_total} "id=\'[dict get $sdata id]\'" list]
			foreach val $result var [list cost_product cost_tax cost_ship cost_other cost_total] {
				dict set tbl_line $var $val
			}
			dict set sdata stock_lines [dict create 0 $tbl_line]
			


			$::daidze_main load_main stock edit_movement_on_stock.xml $::main 1
			$::daidze_main update_form $sdata
		}

	} else {
		tk_messageBox -icon info -type ok -message "[::msgcat::mc "Select entry to edit."]"
	}
}

proc edit_selected_line {table_var} {
	set sdata [dict create]
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]
	set vars {qty fk_product stock_movement_type cost_product cost_tax cost_ship cost_other cost_total}
	set row [$my_tablelist rowcget [$my_tablelist curselection] -text]
	foreach key $vars value $row {
		dict set sdata $key $value
	}
	set new_stock_l [top_win "Edit stock line"]
	set stock_line_frame [ttk::frame $new_stock_l.top]
	set ::daidze_nstline [xml_wrapper new]
	pack $stock_line_frame
	
	$::daidze_nstline load_main stock edit_stock_line.xml $stock_line_frame
	$::daidze_nstline update_form $sdata
}

proc update_stock_line {} {
	set values [$::daidze_nstline get_values]

	set msg_list [dict create qty "Quantity is obligatory." fk_product "Product field is obligatory." stock_movement_type "Movement type is obligatory."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {

		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 
	
	if {[lsearch -not -exact $flags 1] == "-1"} {
		dict create stock_lines $values
		$::daidze_main update_form [dict create stock_lines [dict create 0 $values] table_clear 1]
		destroy .topwin
	}
}

proc update_stock_movement {} {
	[dict get [$::daidze_main get_fdata] stock_lines] selection set 0 end
	set values [$::daidze_main get_values]

	set msg_list [dict create fk_warehouse "Warehouse name is obligatory." mov_date "Date is obligatory." stock_lines "Add at least one movement"]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		set stock_lines [dict get $values stock_lines]
		foreach line [dict values $stock_lines] {
			set tmp [dict create]
			set mov_type [lindex [$::db select_all stock_movement_type id "description=\'[dict get $line stock_movement_type]\'"] 1]
			dict append tmp id [dict get $values id]
			dict append tmp fk_warehouse [dict get $values fk_warehouse]
			dict append tmp mov_date [dict get $values mov_date]
			dict append tmp mov_time [dict get $values mov_time]
			dict append tmp fk_product [dict get $line fk_product]
			dict append tmp qty [dict get $line qty]
			dict append tmp stock_movement_type [lindex $mov_type 1]
			dict append tmp cost_product [dict get $line cost_product]
			dict append tmp cost_tax [dict get $line cost_tax]
			dict append tmp cost_ship [dict get $line cost_ship]
			dict append tmp cost_other [dict get $line cost_other]
			dict append tmp cost_total [dict get $line cost_total]

			$::db update_all stock_current $tmp
		}
		$::daidze_main load_main stock stock.xml $::main 1
	}

}